// pages/profileTab/mywallet/mywallet.js
const app = getApp();
const appData = app.globalData;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    balance: 0.00,
    table_msg: [],
    hiddenmodalput: true,
    account:''

        //可以通过hidden是否掩藏弹出框的属性，来指定那个弹出框
  },

  //点击按钮痰喘指定的hiddenmodalput弹出框

  accountInput: function (e) {
    this.setData({
     account: e.detail.value
    })
  },

  modalinput: function () {

    this.setData({

      hiddenmodalput: !this.data.hiddenmodalput

    })

  },

  //取消按钮

  cancel: function () {

    this.setData({

      hiddenmodalput: true

    });

  },

  //确认

  // confirm: function () {

  //   this.setData({
  //     hiddenmodalput: true
  //   })

  // },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(appData.openid)
    wx.request({
      url: 'https://www.enjoyyz.com:8443/yinzuo/acountInf',
      data: {
        openid: appData.openid

        // 'sdhh444'
      },
      success(res) {
        console.log(res)
        console.log(res.data.chargeInf)
        for (var i = 0; i < res.data.chargeInf.length; i++) {
          res.data.chargeInf[i].date = new Date(res.data.chargeInf[i].rechargeTime).toLocaleDateString().replace(/\//g, '.');
          console.log(res.data.chargeInf[i].date)
        }
        that.setData({
          balance: res.data.accountBalance,
          table_msg: res.data.chargeInf
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  doPay(e) {
    this.setData({
      hiddenmodalput: true
    })
    console.log(this.data.account,'this.data.account')
    var account = this.data.account*10*10;
    var account1 = this.data.account 
    console.log(account, 'account')
    var arr = new Array(15);//用于存放随机数
    var randomNumber = '';//存放随机数
    for (var i = 0; i < arr.length; i++)
      arr[i] = parseInt(Math.random() * 10);
    var flag = 0;
    for (var i = 0; i < arr.length - 1; i++) {
      for (var j = i + 1; j < arr.length; j++) {
        if (arr[i] == arr[j]) { flag = 1; break; }
      }
      if (flag) break;
    }
    for (var i = 0; i < arr.length; i++) {
      randomNumber += arr[i];
    }
    console.log(randomNumber);
    wx.request({
      url: 'https://www.enjoyyz.com:8443/yinzuo/pay/createOrder', 
      method: 'POST',
      data: {
        body: '胤佐游戏',
        outTradeNo: randomNumber,
        totalFee: account,
        spbillCreateIp: '106.52.53.180',
        notifyUrl: 'https://www.enjoyyz.com:8443/yinzuo/pay/notify/order',
        tradeType: 'JSAPI',
        openid: appData.openid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log("在充值页获取苏哥的openid是：", appData.openid);
        if (res.data.appId) {
          let payData = res.data
          wx.requestPayment({
            timeStamp: payData.timeStamp,
            nonceStr: payData.nonceStr,
            package: payData.packageValue,
            signType: 'MD5',
            paySign: payData.paySign,
            success(res) {
              console.log(res)
              wx.showModal({
                title: '支付成功',
                content: '您已成功支付'+account1+'元',
              })
              
              wx.switchTab({
                url: '/pages/homeTab/homeTab',
              })
            },
            fail(res) { }
          })
        }
        console.log(res.data,'res.data')
      }
    })
  }
})